package com.zprmb.udzt.fragment;

import java.text.DecimalFormat;
import java.util.ArrayList;

import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.zprmb.udzt.LoginActivity;
import com.zprmb.udzt.R;
import com.zprmb.udzt.adapter.Hongbao_Adapter;
import com.zprmb.udzt.bean.MyBean;
import com.zprmb.udzt.util.Constant;
import com.zprmb.udzt.util.VolleryUtil;

public class My_Red_fragment extends Fragment implements
		OnRefreshListener2<ListView> {
	private String token;
	private String title;
	private TextView tv_total_red, red_size, tv_total_red_num, tv_red_size_num;
	private PullToRefreshListView lv_product_fragment;
	private ListView lv;
	private ListView actualListView;
	
	private Hongbao_Adapter adapter;
	private Hongbao_Adapter adapter2;
	private int page = 1;
	private ImageView img_none;
	private int totalpage;
	private RelativeLayout red_nav;
	private ArrayList<MyBean> list = new ArrayList<MyBean>();
	private ArrayList<MyBean> list2 = new ArrayList<MyBean>();
	private ArrayList<MyBean> list3 = new ArrayList<MyBean>();
	private ArrayList<MyBean> list4 = new ArrayList<MyBean>();
	DecimalFormat df = new DecimalFormat("###0.00");

	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.my_red_fragment, null);
		tv_total_red = (TextView) view.findViewById(R.id.tv_total_red);
		img_none=(ImageView)view.findViewById(R.id.iv_v4_nonetwork);
		red_size = (TextView) view.findViewById(R.id.red_size);
		tv_total_red_num = (TextView) view.findViewById(R.id.total_red_num);
		tv_red_size_num = (TextView) view.findViewById(R.id.red_size_num);
		lv_product_fragment = (PullToRefreshListView) view
				.findViewById(R.id.lv_product_fragment);
		actualListView = lv_product_fragment.getRefreshableView();
		registerForContextMenu(actualListView);
		red_nav = (RelativeLayout) view.findViewById(R.id.red_nav);
		red_nav.setVisibility(View.INVISIBLE);
		lv_product_fragment.setOnRefreshListener(this);
		lv_product_fragment.setMode(Mode.BOTH);
		initData();
		return view;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		token = getArguments().getString("token");
		title = getArguments().getString("title");
	}

	private void initData() {
		// TODO Auto-generated method stub
		VolleryUtil volleryUtil = new VolleryUtil() {

			@Override
			public void onUpDate(JSONObject jsonObject) {
				// TODO Auto-generated method stub

				if (jsonObject.optInt("success") == 1) {
					if (title.equals("可用")) {
						red_nav.setVisibility(View.VISIBLE);
						Log.e("可用", jsonObject.toString());
						red_nav.setVisibility(View.VISIBLE);
						tv_total_red.setText("可用红包金额(元)");
						red_size.setText("已用红包金额(元)");
						Gson gson = new Gson();
						JSONObject json1 = jsonObject.optJSONObject("data");
						JSONObject json2 = json1.optJSONObject("gitftCardList");

						tv_total_red_num
								.setText(df.format(Double.parseDouble(json1
										.optString("avaliableAmount"))));

						tv_red_size_num.setText(df.format(Double
								.parseDouble(json1.optString("usedAmount"))));
						if (json2 != null) {

							list = gson.fromJson(json2.optString("resultList"),
									new TypeToken<ArrayList<MyBean>>() {
									}.getType());
							if (list != null) {

								list2.addAll(list);
								adapter = new Hongbao_Adapter(list2,
										getActivity(), title);
								lv_product_fragment.setAdapter(adapter);
								adapter.notifyDataSetChanged();
								actualListView.setSelection(list2.size() - 5);
								if(list2.size()<1){
									lv_product_fragment.setVisibility(View.GONE);
									img_none.setVisibility(View.VISIBLE);
								}else{
									lv_product_fragment.setVisibility(View.VISIBLE);
									img_none.setVisibility(View.GONE);
								}
							}
						}

					} else {
						Log.e("过期", jsonObject.toString());
						Gson gson = new Gson();
						JSONObject json1 = jsonObject.optJSONObject("data");
						JSONObject json2 = json1.optJSONObject("gitftCardList");
						red_nav.setVisibility(View.GONE);
						if (json2 != null) {
                           
							list3 = gson.fromJson(
									json2.optString("resultList"),
									new TypeToken<ArrayList<MyBean>>() {
									}.getType());
							if(list3!=null){
								
								list4.addAll(list3);
								adapter2 = new Hongbao_Adapter(list4,
										getActivity(), title);
								lv_product_fragment.setAdapter(adapter2);
								adapter2.notifyDataSetChanged();
								actualListView.setSelection(list2.size() - 5);
								if(list4.size()<1){
									lv_product_fragment.setVisibility(View.GONE);
									img_none.setVisibility(View.VISIBLE);
								}else{
									lv_product_fragment.setVisibility(View.VISIBLE);
									img_none.setVisibility(View.GONE);
								}
							}
						}
					}
				} else {
					if (jsonObject.optString("comment").equals("please login")) {
						Intent intent = new Intent(getActivity(),
								LoginActivity.class);
						startActivity(intent);
					} else {
						Toast.makeText(getActivity(),
								jsonObject.optString("comment"), 2000).show();
					}
				}

			}

			@Override
			public void onError() {
				// TODO Auto-generated method stub

			}
		};
		volleryUtil.setHeaders("token", token);
		volleryUtil.setRequestMethod(0);
		volleryUtil.setShow(true);
		if (title.equals("可用")) {

			volleryUtil.execute(getActivity(), Constant.URL
					+ "/giftCardApi/myGiftCard/available/?pageNo=" + page
					+ "&pageSize=5", "");
		} else {
			volleryUtil.execute(getActivity(), Constant.URL
					+ "/giftCardApi/myGiftCard/overdue?pageNo=" + page
					+ "&pageSize=5", "");
		}
	}

	public static My_Red_fragment newInstance(int position, String string,
			String title) {

		My_Red_fragment uf = new My_Red_fragment();
		Bundle bundle = new Bundle();
		bundle.putString("token", string);
		bundle.putString("title", title);
		uf.setArguments(bundle);

		return uf;
	}

	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			if (title.equals("可用")) {
				list.clear();
				if (msg.what == 1) {
					page = 1;
					list2.clear();
					initData();
				} else {
					page += 1;
					initData();

				}
				Toast.makeText(getActivity(), "刷新成功", 2000).show();
				lv_product_fragment.onRefreshComplete();
			} else {
				list3.clear();
				if (msg.what == 1) {
					page = 1;
					list4.clear();
					initData();
				} else {
					page += 1;
					initData();

				}
				Toast.makeText(getActivity(), "刷新成功", 2000).show();
				lv_product_fragment.onRefreshComplete();
			}

		};
	};

	@Override
	public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
		// TODO Auto-generated method stub
		handler.sendEmptyMessage(1);
	}

	@Override
	public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
		// TODO Auto-generated method stub
		handler.sendEmptyMessage(2);
	}
}
